<?php


namespace App\Http\Controllers\Estate;


use App\Http\Controllers\Controller;
use App\Models\HomeBanner;
use App\Models\HouseModel;
use App\Services\HouseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class HouseController extends Controller
{
    public $service;

    public function __construct()
    {
        $this->service = new HouseService();
    }

    /**
     * 列表
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $user  = $request->user();
        $param = $request->query();
        //查询当前物业自己添加的数据
        $where['estate_id'] = $user['estate_id'];
        return $this->success($this->service->getHouseList($where, $param));
    }

    /**
     * 新增
     * @param Request $request
     * @return array
     */
    public function store(Request $request)
    {
        $user = $request->user();
        $data = $request->post();
        $result = $this->service->addHouse($data,$user['estate_id']);
        if ($result === false) {
            return $this->error($this->service->getMessage());
        }

        return $this->success('添加成功');

    }

    /**
     * 获取详情
     * @param $id
     * @return array
     */
    public function show(HouseModel $houseModel, $h_id)
    {
        return $this->success($houseModel->where('h_id', $h_id)->first());
    }

    /**
     * 编辑
     * @param Request $request
     * @return array
     */
    public function update(Request $request, $id)
    {
        $data                 = $request->post();
        $info = HouseModel::find($id);
        if (!$info) {
            return $this->error('该数据不存在');
        }
        $info->c_id = $data['community_id'];
        $info->house_num = $data['house'];
        try {
            $info->save();
        } catch (\Throwable $e) {
            return $this->error('该楼层已存在，不可重复添加');
        }
        return $info->save() ? $this->success('修改成功!') : $this->error('修改失败!');
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function destroy(HouseModel $houseModel, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        //查询是否存在单元信息，存在则不允许删除

        return $houseModel->destroy($idArray) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * Notes: 获取小区的所有楼栋
     * User: lstink
     * Date: 2021/2/4
     * Time: 4:21 下午
     */
    public function getAllHouseByCommunity(HouseService $houseService, $id)
    {
        return $this->success($houseService->getHouseListByCommunity($id));
    }

    /**
     * Notes: 获取楼号的所有单元
     * User: lstink
     * Date: 2021/2/4
     * Time: 4:21 下午
     */
    public function getAllElementByHouse(HouseService $houseService, $id)
    {
        return $this->success($houseService->getElementListByHouse($id));
    }

    /**
     * Notes: 获取单元的所有楼层
     * User: lstink
     * Date: 2021/2/4
     * Time: 4:21 下午
     */
    public function getAllFloorByElement(HouseService $houseService, $id)
    {
        return $this->success($houseService->getFloorListByElement($id));
    }
}
